package cz.cuni.amis.episodic.bayes.memories;

import cz.cuni.amis.episodic.bayes.utils.MemChart;
import cz.cuni.amis.episodic.bayes.utils.NodeSnapshot;
import cz.cuni.amis.episodic.bayes.utils.Utils;
import cz.cuni.amis.episodic.bayes.utils.chart.GraphPaintingDevice;
import cz.cuni.amis.episodic.dybanem.DBN;
import cz.cuni.amis.episodic.dybanem.Evidence;

import java.awt.Point;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ik
 */
public class PointSurpriseMemoryCreator extends KLBasedMemoryCreator {

    public PointSurpriseMemoryCreator(GraphPaintingDevice graphPaintingDevice) {
        super(graphPaintingDevice, "retro_point");
    }

    
    @Override
    public MemoryElement createMemoryInternal(String[] targets, NodeSnapshot[] trueSnapshots, DBN dbn, Collection<? extends Evidence> evidences, int iteration) {

        // compute KL from current knowledge to the real state
        //double[][] kl = Utils.computeKL(currentSnapshots, trueSnapshots);
    	double[][] kl = Utils.computeKL(trueSnapshots, currentSnapshots);

        // find max index
        Object[] maxRes = Utils.maxIndex(kl);
        Point idx = (Point) maxRes[0];
        double[] distr = trueSnapshots[idx.x].getProbability()[idx.y];
        // TODO temp
//            distr = Utils.maxToOneVector(distr);
        String node = targets[idx.x];

        // draw graph 
        if (graphPaintingDevice != null) {
            // we set this only because of drawing, NaN has specal color and here it indicates the max value
            kl[idx.x][idx.y] = Double.NaN;
            drawKLGraph(kl, targets, iteration);
        }

        // remember it
        return new MemoryElement(node, idx.y, idx.y, (double) maxRes[1], dbn.getNet().getOutcomeIds(node)[Utils.maxIndex(distr)]);
    }
}
